home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / lang / SmallEiffel.lha / SmallEiffel / misc / benchmarks / gc / LkRORnd / std_rand.e < prev   
Encoding:
Text File  |  1998-12-22  |  1.9 KB  |  98 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://www.loria.fr/SmallEiffel
  11. --
  12. class STD_RAND
  13.    -- Press' standard generator, which uses the minimal standard 
  14.    -- and then uses shuffling to break up short order corelations.
  15.    
  16. creation with_seed
  17.  
  18. feature {NONE}
  19.    
  20.    seed:INTEGER
  21.  
  22.    ia: INTEGER is 16807;
  23.  
  24.    im: INTEGER is 2147483647;
  25.  
  26.    iq: INTEGER is 127773;
  27.  
  28.    ir: INTEGER is 2836;
  29.  
  30.    iv: ARRAY[INTEGER];
  31.  
  32.    ntab: INTEGER is 32;
  33.  
  34.    iy: INTEGER;
  35.  
  36. feature {NONE}
  37.  
  38.    min_next is
  39.       local
  40.      k: INTEGER;
  41.       do
  42.      k := seed // iq;
  43.      seed := ia * (seed -k * iq) - ir * k;
  44.      if seed < 0 then
  45.         seed := seed + im;
  46.      end;
  47.       end;
  48.  
  49.    with_seed(seed_value:INTEGER) is
  50.       require
  51.      valid_seed: seed_value > 0 and seed_value < im
  52.       local
  53.      i: INTEGER;
  54.       do
  55.      seed := seed_value;
  56.      min_next;
  57.      !!iv.make(1,ntab);
  58.      from 
  59.         i := 1;
  60.      until 
  61.         i > 7
  62.      loop
  63.         min_next;
  64.         i := i + 1;
  65.      end;
  66.      from
  67.         i := ntab;
  68.      until
  69.         i = 0
  70.      loop
  71.         iv.put(seed,i);
  72.         min_next;
  73.         i := i - 1;
  74.      end
  75.      iy := iv.item(1);
  76.      next;
  77.       end
  78.  
  79. feature
  80.  
  81.    next is
  82.       local
  83.      tmp:INTEGER
  84.       do
  85.      tmp := iy \\ ntab + 1;
  86.      min_next;
  87.      iy := iv.item(tmp);
  88.      iv.put(seed,tmp);
  89.       end
  90.  
  91.    last_integer(n:INTEGER):INTEGER is
  92.       do
  93.      Result := iy \\ n + 1;
  94.       end
  95.  
  96. end -- STD_RAND
  97.  
  98.